<?php
/*
 +----------------------------------------------------------------------
 + Title        : SysSetingLogic 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2021/2/28 22:01
 + Last-time    : 2021/2/28 22:01+ chen2
 + Desc         : SysSetingLogic
 +----------------------------------------------------------------------
*/


namespace app\common\service;

use app\common\model\Setting;
use app\user\model\UserShare;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

class SysSettingService
{
    public static function tree()
    {
        $list = Setting::select();
        return $list;
    }


    /**
     *
     * @param $id
     *
     * @return array|bool|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author : Randy_chen
     * @Date   : 2021/3/2
     * @Time   : 22:39
     */
    public function getSysSettingItem($key)
    {
        $item = Setting::where('key', $key)
                       ->findOrFail();
        return $item;
    }

    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function getSysSettingList($extendQueryHelper, $data) : \think\Collection|\think\Paginator|array
    {
        $query = Setting::build()
                          ->where($extendQueryHelper->where)
                          ->order($extendQueryHelper->sort, $extendQueryHelper->order);
        return $extendQueryHelper->is_page ? $query->_paginate() : $query->select();
    }

    /**
     *
     * @param $params
     *{"send_seting": {"send_hour": 101, "Interval_hour": 3}, "confirm_seting": {"delayed_day": 1}, "interval_seting": {"delayed_hour": 5}}
     *
     * @return bool
     * @throws \Exception
     * @author : Randy_chen
     * @Date   : 2021/3/2
     * @Time   : 22:40
     */
    public function setSysSettingItem($data, $key) : bool
    {
        Db::startTrans();
        try {
            $config = Setting::build()
                             ->where('key', $key)
                             ->findOrFail();
            $config->save($data);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            throw $e;
        }
    }


}